package com.gengzp.system.facade.impl;

import com.gengzp.common.exception.BusinessException;
import com.gengzp.common.model.role.entity.SysRole;
import com.gengzp.system.facade.SysRoleFacade;
import com.gengzp.system.mapper.SysRoleMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName SysRoleFacadeImpl
 * @Description 系统角色门面层实现类
 * @Author gengzp
 * @Date 2025/9/15 16:26
 */
@Service
public class SysRoleFacadeImpl implements SysRoleFacade {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public SysRole searchSysRoleById(Long roleId) {
        if (Objects.isNull(roleId)) {
            throw BusinessException.get("角色id不能为空");
        }

        return sysRoleMapper.selectSysRoleById(roleId);
    }

    @Override
    public List<SysRole> searchSysRolesByIds(Collection<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return Lists.newArrayList();
        }

        return sysRoleMapper.selectSysRolesByIds(roleIds);
    }

    @Override
    public List<SysRole> searchAllSysRoles() {
        return sysRoleMapper.selectAllSysRoles();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editSysRoleById(SysRole sysRole) {
        if (Objects.isNull(sysRole)) {
            throw BusinessException.get("角色信息不能为空");
        }

        sysRoleMapper.updateSysRoleById(sysRole);
    }

    @Override
    public SysRole searchSysRoleByRoleName(String roleName) {
        if (StringUtils.isBlank(roleName)) {
            throw BusinessException.get("角色名称不能为空");
        }

        return sysRoleMapper.selectSysRoleByName(roleName);
    }

}
